import datetime
from fastapi import FastAPI
from fastapi.concurrency import asynccontextmanager
from app.dependencies.init_databases import db


@asynccontextmanager
async def lifespan(app: FastAPI):
    await db.connect()
    print("Database connected.", datetime.datetime.now())
    yield
    await db.disconnect()
    print("Database disconnected.",datetime.datetime.now())


def init_dependencies(app: FastAPI):
    from app.dependencies.init_routes import init_rotues

    init_rotues(app)


def create_admin():
    app = FastAPI(lifespan=lifespan)
    # 路由
    from app.api.admin.controller import create_admin_router
    app.include_router(create_admin_router())

    # 依赖
    init_dependencies(app)
    # 中间件
    return app
